package com.yuzhi.fine.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.mikepenz.fontawesome_typeface_library.FontAwesome;
import com.mikepenz.itemanimators.AlphaCrossFadeAnimator;
import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import com.yuzhi.fine.R;
import com.yuzhi.fine.fragment.ListFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends BaseFragmentActivity {


    private static final String TAG = MainActivity.class.getSimpleName();
    private static final String CURR_INDEX = "currIndex";
    private static int currIndex = 0;
    private Drawer result = null;
    private AccountHeader headerResult = null;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.fragment_container)
    FrameLayout mFragmentContainer;

//    private RadioGroup group;

    //    private ArrayList<String> fragmentTags;
    private FragmentManager fragmentManager;
    private String[] mFragmentTags;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
        initData(savedInstanceState);
        initView();

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(CURR_INDEX, currIndex);
    }

    private void initData(Bundle savedInstanceState) {
        mFragmentTags = getResources().getStringArray(R.array.category);
//        fragmentTags = new ArrayList<>(Arrays.asList("HomeFragment", "ImFragment", "InterestFragment", "MemberFragment"));
        currIndex = 0;
        if (savedInstanceState != null) {
            currIndex = savedInstanceState.getInt(CURR_INDEX);
            hideSavedFragment();
        }
    }

    private void hideSavedFragment() {
        Fragment fragment = fragmentManager.findFragmentByTag(mFragmentTags[currIndex]);
        if (fragment != null) {
            fragmentManager.beginTransaction().hide(fragment).commit();
        }
    }

    private void initView() {
        mToolbar.setTitle(mFragmentTags[0]);
        setSupportActionBar(mToolbar);

        headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withTranslucentStatusBar(true)
                .addProfiles(new ProfileDrawerItem().withName("Loofer").withIcon(R.drawable.ic_launcher).withIdentifier(100))
                .withHeaderBackground(R.drawable.header)
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean current) {
                        Toast.makeText(MainActivity.this, current + "", Toast.LENGTH_SHORT).show();
                        return false;
                    }
                }).build();

        result = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(mToolbar)
                .withHasStableIds(true)
                .withItemAnimator(new AlphaCrossFadeAnimator())
                .withAccountHeader(headerResult)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName(mFragmentTags[0]).withIcon(MaterialDesignIconic.Icon.gmi_view_comfy).withIdentifier(0),
                        new PrimaryDrawerItem().withName(mFragmentTags[1]).withIcon(MaterialDesignIconic.Icon.gmi_mood).withIdentifier(1),
                        new PrimaryDrawerItem().withName(mFragmentTags[2]).withIcon(MaterialDesignIconic.Icon.gmi_android).withIdentifier(2),
                        new PrimaryDrawerItem().withName(mFragmentTags[3]).withIcon(MaterialDesignIconic.Icon.gmi_apple).withIdentifier(3),
                        new PrimaryDrawerItem().withName(mFragmentTags[4]).withIcon(MaterialDesignIconic.Icon.gmi_collection_video).withIdentifier(4),
                        new PrimaryDrawerItem().withName(mFragmentTags[5]).withIcon(MaterialDesignIconic.Icon.gmi_language_javascript).withIdentifier(5),
                        new PrimaryDrawerItem().withName(mFragmentTags[6]).withIcon(FontAwesome.Icon.faw_location_arrow).withIdentifier(6),
                        new PrimaryDrawerItem().withName(mFragmentTags[7]).withIcon(MaterialDesignIconic.Icon.gmi_apps).withIdentifier(7),
                        new PrimaryDrawerItem().withName(mFragmentTags[8]).withIcon(MaterialDesignIconic.Icon.gmi_more).withIdentifier(8)
                ).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        if (drawerItem != null) {
                            switch ((int) drawerItem.getIdentifier()) {
                                case 0:
                                    currIndex = 0;
                                    break;
                                case 1:
                                    currIndex = 1;
                                    break;
                                case 2:
                                    currIndex = 2;
                                    break;
                                case 3:
                                    currIndex = 3;
                                    break;
                                case 4:
                                    currIndex = 4;
                                    break;
                                case 5:
                                    currIndex = 5;
                                    break;
                                case 6:
                                    currIndex = 6;
                                    break;
                                case 7:
                                    currIndex = 7;
                                    break;
                                case 8:
                                    currIndex = 8;
                                    break;
                                default:
                                    break;
                            }
                            showFragment();
//                            mToolbar.setTitle(mFragmentTags[currIndex]);
                        }
                        return false;
                    }
                }).build();
        showFragment();

    }

    private void showFragment() {
//        if (currIndex == 3) {
//            UIHelper.showLogin(MainActivity.this);
//        }

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(mFragmentTags[currIndex]);
        if (fragment == null) {
            fragment = instantFragment(currIndex);
        }
        for (int i = 0; i < mFragmentTags.length; i++) {
            Fragment f = fragmentManager.findFragmentByTag(mFragmentTags[i]);
            if (f != null && f.isAdded()) {
                fragmentTransaction.hide(f);
            }
        }
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.fragment_container, fragment, mFragmentTags[currIndex]);
        }
        mToolbar.setTitle(mFragmentTags[currIndex]);
        fragmentTransaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }

    private Fragment instantFragment(int currIndex) {
        Log.d("---", currIndex + "");
        switch (currIndex) {
            case 0:
                return ListFragment.newInstance("all");
            case 1:
                return ListFragment.newInstance(mFragmentTags[1]);
            case 2:
                return ListFragment.newInstance(mFragmentTags[2]);
            case 3:
                return ListFragment.newInstance(mFragmentTags[3]);
            case 4:
                return ListFragment.newInstance(mFragmentTags[4]);
            case 5:
                return ListFragment.newInstance(mFragmentTags[5]);
            case 6:
                return ListFragment.newInstance(mFragmentTags[6]);
            case 7:
                return ListFragment.newInstance(mFragmentTags[7]);
            case 8:
                return ListFragment.newInstance(mFragmentTags[8]);
            default:
                return null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
